package com.qztc.news_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.qztc.news_end_core.util.MD5Parse;
import com.qztc.news_end_dto.dto.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
@Tag(name = "用户接口")
public class UserController {

    @Operation(summary = "查询所有用户")
    @GetMapping("/get")
    public List<User> get(){
        return new User().selectAll();
    }

    @Operation(summary = "根据用户id删除用户")
    @DeleteMapping("/deletebyuserid")
    public boolean delete(@RequestBody User user){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("userid",user.getUserid());
        return user.delete(wrapper);
    }

    @Operation(summary = "禁用用户")
    @PutMapping("/update_isuse_0")
    public boolean updateIsUse0(@RequestBody User user){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("userid",user.getUserid());
        user.setIsuse(0);
        return user.update(wrapper);
    }

    @Operation(summary = "启用用户")
    @PutMapping("/update_isuse_1")
    public boolean updateIsUse1(@RequestBody User user){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("userid",user.getUserid());
        user.setIsuse(1);
        return user.update(wrapper);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public boolean register(@RequestBody User user){
        user.setUserpwd(MD5Parse.encryptMD5(user.getUserpwd()));
        user.setIsuse(1);
        return user.insert();
    }

    @Operation(summary = "获取某一个用户信息")
    @GetMapping("/getbyuserid")
    public User getByUserId(@RequestParam Integer userid){
        return new User().selectById(userid);
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public User login(@RequestBody User user) {
        user.setUserpwd(MD5Parse.encryptMD5(user.getUserpwd()));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("userpwd", user.getUserpwd());
        queryWrapper.eq("isuse", 1);
        return new User().selectOne(queryWrapper);
    }

    @Operation(summary = "修改用户信息")
    @PutMapping("/update")
    public boolean update(@RequestBody User user){
        user.setUserpwd(MD5Parse.encryptMD5(user.getUserpwd()));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",user.getUserid());
        return user.update(queryWrapper);
    }
}
